package com.focus.pay.wx.action.combine;

import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.focus.pay.wx.config.WxPayConfig;
import com.focus.pay.wx.constant.WxPayConstant;
import com.focus.pay.core.annation.PayOrderHandler;
import com.focus.pay.core.enums.ChannelTypeEnum;
import com.focus.pay.core.enums.Version;
import com.focus.pay.core.exception.PayException;
import com.focus.pay.core.pay.PayOrderInterface;
import com.focus.pay.core.utils.HttpClientUtils;
import com.focus.pay.wx.model.order.v3.WxPayModelUnifiedResponse;
import com.focus.pay.wx.model.order.v3.combine.WxPayCommonCombine;
import com.focus.pay.wx.model.order.v3.combine.WxPayModelUnifiedCombine;
import com.focus.pay.wx.utils.WxPayUtils;

import java.util.Map;

/**
 * @author 赵参谋
 * @version $
 * =========================================================================
 * 变更履历：
 * -------------------------------------------------------------------------
 * 变更编号     变更时间    变更人   变更原因    变更内容
 * -------------------------------------------------------------------------
 * <p>
 * @description： 合单APP支付
 * @date 2022/1/7 10:32
 **/
@PayOrderHandler(channelType = ChannelTypeEnum.WxType.App,version = Version.VERSION_3,extension = ChannelTypeEnum.channelExtension.Combine)
public class WxPayOrderCombineApp extends com.focus.pay.wx.action.v3.WxPay implements PayOrderInterface<WxPayModelUnifiedResponse, WxPayModelUnifiedCombine, WxPayConfig> {

    @Override
    public WxPayModelUnifiedResponse payOrder(WxPayModelUnifiedCombine wxPayModelCombineUnified, WxPayConfig wxPayConfig) throws PayException {
        DeployParams deployParams = super.instanceDeployParams(WxPayConstant.WxPayPathV3.Pay_COMBINE_ORDER_UNIFIED,new String[]{ChannelTypeEnum.WxType.App});
        String res = HttpClientUtils.doPost(
                super.buildRequestPath(deployParams),
                buildBody(wxPayModelCombineUnified,wxPayConfig),
                buildHead(Method.POST,wxPayModelCombineUnified,wxPayConfig,deployParams));
        return this.handle(res,wxPayConfig);
    }

    /**
     * 返回数据处理
     * @param res
     * @param wxPayConfig
     * @return
     * @throws Exception
     */
    private WxPayModelUnifiedResponse handle(String res, WxPayConfig wxPayConfig) throws PayException {
        WxPayModelUnifiedResponse wxPayModelUnifiedResponse = super.resolvingResult(res, WxPayModelUnifiedResponse.class);
        wxPayModelUnifiedResponse.setPageData(WxPayUtils.AppRecall(wxPayModelUnifiedResponse,wxPayConfig, Version.VERSION_3));
        return wxPayModelUnifiedResponse;
    }

    /**
     * 构建请求体
     */
    private  <T extends WxPayCommonCombine> String buildBody(T t, WxPayConfig wxPayConfig){
        t.buildParam(t,wxPayConfig);
        return JSONUtil.toJsonStr(t);
    }

    /**
     * 构建请求头
     */
    private  <T extends WxPayCommonCombine> Map buildHead(Method method, T t, WxPayConfig wxPayConfig, DeployParams deployParams) throws PayException {
        String route = buildRequestRoute(deployParams);
        return t.buildRequestHead(method,route,t,wxPayConfig);
    }

}
